package com.icode.web.controller.house;

import com.icode.core.dto.house.EquipmentDTO;
import com.icode.core.model.house.Equipment;
import com.icode.core.model.house.EquipmentType;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lb
 * Date: 16-1-12
 * Time: 下午2:20
 * To change this template use File | Settings | File Templates.
 */
@Controller("EquipemntSupportingConrtoller")
@RequestMapping("/equipemntsupporting")
public class EquipemntSupportingConrtoller {
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String Equipments() throws Exception {
        return "back/houseBase/equipemntsupporting";
    }

    @RequestMapping(value = "listdata", method = RequestMethod.GET)
    @ResponseBody
    public HashMap listdata() throws Exception {
        HashMap<String, List<EquipmentDTO>> hashMap = new HashMap<>();
        hashMap.put("EQUIPMENT", houseService.getEquipmentDTOs(EquipmentType.EQUIPMENT));
        hashMap.put("SUPPORTING", houseService.getEquipmentDTOs(EquipmentType.SUPPORTING));
        return hashMap;
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public EquipmentDTO add(String name, String type) {
        EquipmentDTO equipmentDTO = null;
        Equipment equipment = houseService.addEquipment(name, type);
        if (equipment != null) {
            equipmentDTO = EquipmentDTO.toDTO(equipment);
        }
        return equipmentDTO;
    }

    @RequestMapping(value = "updata", method = RequestMethod.POST)
    @ResponseBody
    public Object updata(String guid, String name) {
        Object object = houseService.updataEquipment(guid, name);
        return object;
    }

    @RequestMapping(value = "delete/{guid}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable String guid) {
        houseService.deleteEquipmentByGuid(guid);
        return true;
    }
}
